define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'count/monitor/index' + location.search,
                    add_url: 'count/monitor/add',
                    edit_url: 'count/monitor/edit',
                    del_url: 'count/monitor/del',
                    multi_url: 'count/monitor/multi',
                    table: 'goods_monitor',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods.title', title: __('Goods.title')},
                        {field: 'goods.specs', title: __('Goods.specs')},
                        {field: 'storehouse.name', title: __('Storehouse.name')},
                        {field: 'stock_num', title: __('Stock_num')},
                        {field: 'upper_num', title: __('upper_num')},
                        {field: 'keep_num', title: __('keep_num')},
                        {field: 'error_msg', title: __('error_msg')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'is_monitor', title: __('Is_monitor')},
                        {field: 'is_error', title: __('报警'),  searchList: {"1":__('报警'),"0":__('正常')},formatter: Controller.api.formatter.customs},
                        {field: 'is_monitor', title: __('监控'), operate: false, formatter: Controller.api.formatter.custom},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="count/monitor/change" data-id="' + row.id + '"><i class="fa ' + (row.is_monitor == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                customs: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="count/monitor/changes" data-id="' + row.id + '"><i class="fa ' + (row.is_error == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});